<?php

namespace app\common\dao\repair;

use think\db\BaseQuery;
use app\common\dao\BaseDao;
use app\common\model\repair\RepairLogModel;

class RepairLogDao extends BaseDao
{

    /**
     * @return RepairLogModel
     */
    protected function getModel(): string
    {
        return RepairLogModel::class;
    }


    public function search(array $where, int $propertyId = null)
    {
        return $this->getModel()::getDB()
            ->when($propertyId !== null, function ($query) use ($propertyId) {
                $query->where('property_id', $propertyId);
            })
            ->when(isset($where['repair_id']) && $where['repair_id'] !== '', function ($query) use ($where) {
                $query->where('repair_id', (int)$where['repair_id']);
            })
            ->when(isset($where['user_id']) && $where['user_id'] !== '', function ($query) use ($where) {
                $query->where('user_id', (int)$where['user_id']);
            })
            ->when(isset($where['keywords']) && $where['keywords'] !== '', function ($query) use ($where) {
                $query->whereLike('remark', '%' . trim($where['keywords']) . '%');
            })
            ->when(isset($where['add_time']) && $where['add_time'] !== '', function ($query) use ($where) {
                $this->timeSearchBuild($query, $where['add_time'], 'add_time');
            });
    }


    /**
     * 删除报修日志
     * @param int $repairId
     * @return bool
     */
    public function clearRepairLog(int $repairId)
    {
        return $this->getSearch([
            'repair_id' => $repairId
        ])->delete();
    }


}
